package com.lianliankan.llkan;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.example.llkan.R;
import com.lianliankan.util.ButtonSelector;
import com.lianliankan.util.ExtendMethod;

import java.util.List;

public class WelcomeActivity extends Activity {
	private ImageButton loginButton;
	private ImageButton registerButton;
	private Intent intent = new Intent("com.angel.Android.MUSIC");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		findView();
		init();
	}

	private void findView() {
		loginButton = (ImageButton) findViewById(R.id.welcomeLoginButton);
		registerButton = (ImageButton) findViewById(R.id.welcomeRegisterButton);
	}

	public Intent getExplicitIntent(Context context, Intent implicitIntent) {
		// Retrieve all services that can match the given intent
		PackageManager pm = context.getPackageManager();
		List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
		// Make sure only one match was found
		if (resolveInfo == null || resolveInfo.size() != 1) {
			return null;
		}
		// Get component info and create ComponentName
		ResolveInfo serviceInfo = resolveInfo.get(0);
		String packageName = serviceInfo.serviceInfo.packageName;
		String className = serviceInfo.serviceInfo.name;
		ComponentName component = new ComponentName(packageName, className);
		// Create a new intent. Use the old one for extras and such reuse
		Intent explicitIntent = new Intent(implicitIntent);
		// Set the component to be explicit
		explicitIntent.setComponent(component);
		return explicitIntent;
	}

	private void init() {
		Intent eintent = new Intent(getExplicitIntent(this,intent));
		ButtonSelector bSelector = new ButtonSelector();
	        startService(eintent);//启动背景音乐
		loginButton.setBackgroundDrawable(bSelector.newSelector(
				WelcomeActivity.this, R.drawable.loginbtn,
				R.drawable.loginpress, R.drawable.loginpress,
				R.drawable.loginpress));
		registerButton.setBackgroundDrawable(bSelector.newSelector(
				WelcomeActivity.this, R.drawable.registerbtn,
				R.drawable.registerpress, R.drawable.registerpress,
				R.drawable.registerpress));
		loginButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// extendMethod.buttonClickSound(WelcomeActivity.this);
				Intent intent = new Intent(WelcomeActivity.this,
						LoginActivity.class);// Activity切换
				intent.putExtra("action", "login");
				startActivity(intent);

			}

		});
		registerButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// extendMethod.buttonClickSound(WelcomeActivity.this);
				Intent intent = new Intent(WelcomeActivity.this,
						LoginActivity.class);// Activity切换
				intent.putExtra("action", "register");
				startActivity(intent);

			}

		});

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog isExit = new AlertDialog.Builder(this).create();
			isExit.setTitle("系统提示");
			isExit.setMessage("确定要退出吗");
			isExit.setButton("确定", listener);
			isExit.setButton2("取消", listener);
			isExit.show();
		}
		return false;
	}

	DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
				finish();
				break;
			case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Intent eintent = new Intent(getExplicitIntent(this,intent));
		stopService(eintent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.welcome, menu);
		return true;
	}

	protected void onResume() {
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		super.onResume();
	}

}
